"""Contains a decorator for logging."""

__all__ = ['logged']

#_______________________________________________________________________________

import functools
import logging

#_______________________________________________________________________________

if __debug__:

    _logger = logging.getLogger("nxchaos")

    def logged(function):

        @functools.wraps(function)
        def wrapper(*args, **kwargs):

            log = "called: " + function.__name__ + "("
            log += ", ".join(["{0!r}".format(a) for a in args] +
                             ["{0!s}={1!r}".format(k, v)
                             for k, v in kwargs.items()])

            result = exception = None

            try:
                result = function(*args, **kwargs)
                return result

            except Exception as err:
                exception = err

            finally:
                log += ((") -> " + str(result)) if exception is None
                    else ") {0}: {1}".format(type(exception),
                        exception))
                _logger.debug(log)

                if exception is not None:
                    raise exception

        return wrapper

else:
    def logged(function):

        return function
